import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userInfo: "",
    realName: "",
    sender: "",
    deviceInfo: "",
    session_id: 0,
    handle_id: 0,
    status: ""
  },
  mutations: {
    ["LOGIN"]: (state, data) => {
      localStorage.setItem("userInfo", JSON.stringify(data));
      localStorage.setItem("realName", data.realName);
      localStorage.setItem("sender", data.sender);
      localStorage.setItem("username", data.username);
    },
    ["SETDEVICEINFO"]: (state, data) => {
      localStorage.setItem("deviceInfo", JSON.stringify(data));
    },
    ["LOGOUT"]: state => {
      console.log(state);
      localStorage.removeItem("userInfo");
      localStorage.removeItem("realName");
      localStorage.removeItem("sender");
      localStorage.removeItem("username");
      localStorage.removeItem("deviceInfo");
    },
    ["REALNAME"]: state => {
      state.realName = localStorage.getItem("realName");
    },
    ["SENDER"]: state => {
      state.sender = localStorage.getItem("sender");
    },
    ["USERNAME"]: state => {
      state.username = localStorage.getItem("username");
    },
    ["USERINFO"]: state => {
      state.userInfo = JSON.parse(localStorage.getItem("userInfo"))
        ? JSON.parse(localStorage.getItem("userInfo"))
        : {};
    },
    ["DEVICEINFO"]: state => {
      console.log("DEVICEINFO");
      console.log(localStorage.getItem("deviceInfo"));
      state.deviceInfo = JSON.parse(localStorage.getItem("deviceInfo"))
        ? JSON.parse(localStorage.getItem("deviceInfo"))
        : {};
    }
  },
  actions: {
    UserLogin: function({ commit }, data) {
      commit("LOGIN", data);
    },
    UserLogout: function({ commit }) {
      commit("LOGOUT");
    },
    UserInfo: function({ commit }) {
      commit("USERINFO");
    },
    DeviceInfo: function({ commit }) {
      commit("DEVICEINFO");
    },
    SetDeviceInfo: function({ commit }, data) {
      commit("SETDEVICEINFO", data);
    },
    RealName: function({ commit }) {
      commit("REALNAME");
    },
    Username: function({ commit }) {
      commit("USERNAME");
    },
    Sender: function({ commit }) {
      commit("SENDER");
    }
  },
  modules: {}
});
